package middleware

import (
	"github.com/gin-gonic/gin"
	"github.com/golang-jwt/jwt/v4"
	"local.public/innerpkg/logger"
	"local.public/innerpkg/pubconfig"
	"local.public/rsps"
	"strings"
)

type RspInfo struct {
	Code    int         `json:"code"`
	Message string      `json:"msg"`
	Data    interface{} `json:"data,omitempty"`
}

func FirstUpper(s string) string {
	if s == "" {
		return ""
	}
	return strings.ToUpper(s[:1]) + s[1:]
}

func HeaderAuth(ctx *gin.Context) {
	//logger.Debug("进入HeaderAuth...")
	//requestMethod := ctx.Request.Method
	requestUri := ctx.Request.RequestURI
	//logger.Debug("JwtParseInfo method:" + requestMethod + ",URL:" + requestUri)
	if strings.HasSuffix(requestUri, "/ping") { //无须鉴权，pass
		ctx.Next() // 处理请求
		return
	}
	needHeadAuth := true
	//logger.Debug(requestUri + "需要鉴权")
	if !needHeadAuth {
		ctx.Next() // 处理请求
		return
	}

	//{ //优先KeyAuth校验
	//	sKey := FirstUpper(pubconfig.GetConfig().KeyAuth.KeyName)
	//	ApiKeyValues, ok := ctx.Request.Header[sKey]
	//	if ok {
	//		if len(ApiKeyValues) > 0 {
	//			if ApiKeyValues[0] == pubconfig.GetConfig().KeyAuth.KeyValue {
	//				logger.Debug(pubconfig.GetConfig().KeyAuth.KeyName + " keyauth鉴权OK!")
	//				ctx.Next()
	//				return
	//			} else {
	//				errinfo := RspInfo{
	//					Code:    417,
	//					Message: "keyauth鉴权失败",
	//				}
	//				ctx.JSON(200, errinfo)
	//				ctx.Abort()
	//				return
	//			}
	//		}
	//	}
	//}

	//logger.Debug(datastore.GetConfig().KeyAuth.KeyName + " is not exist!")
	//logger.Debug(ctx.Request.Header)
	{
		tokens, ok := ctx.Request.Header["Token"]
		if ok {
			//keyauth
			if len(tokens) > 0 {
				token_s := ctx.Request.Header["Token"][0]
				//logger.Debug("token_s:", token_s)

				token, err := jwt.Parse(token_s, func(token *jwt.Token) (interface{}, error) {
					return []byte(pubconfig.GetConfig().Jwt.Key), nil
				})

				if err != nil {
					logger.Debug("parse token 失败！111111")
					logger.Debug("err:" + err.Error())
					errRspInfo := rsps.ResponseInfo{
						Success:       false,
						Code:          401,
						Message:       "parse token 失败！",
						MessageDetail: err.Error(),
					}
					ctx.JSON(200, errRspInfo)
					ctx.Abort()
					return
				} else {
					if !token.Valid {
						logger.Debug("token无效")
						//errRspInfo := response.ResponseInfo{
						//	ErrCode:    417,
						//	ErrMessage: "token无效",
						//}
						//ctx.JSON(200, errRspInfo)
						//ctx.Abort()
						//return
					} else {
						//logger.Debug("token有效")
						//claim, ok := token.Claims.(jwt.MapClaims)
						//if !ok {
						//	fmt.Println("转为claim失败")
						//} else {
						//	sub := claim["sub"].(string)
						//	fmt.Println("sub is ", sub)
						//}
					}
				}
			}
			// 处理请求
			ctx.Next()
			return
		}

	}

	//errRspInfo := ginrsp.ResponseInfo{
	//	ErrCode:    417,
	//	ErrMessage: "未鉴权",
	//}
	//ctx.JSON(200, errRspInfo)
	//ctx.Abort()
	//return
	// 处理请求
	ctx.Next()

}
